﻿using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace NursingFormSystem.FallNursingMeasuresToPrevent
{
    public partial class HistoryFallNursingMeasuresToPrevent : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 窗体函数
        public HistoryFallNursingMeasuresToPrevent(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.PatientInfo = patientInfo;
            this.LoginUser = loginUser;
            BindControl();
            BindInfo();
        }

        private void BindControl()
        {
            StatDate.DateTime = DateTime.Now.AddDays(-30).Date;
            EndDate.DateTime = DateTime.Now;
            bandedGridView1.Columns[0].Width = 90;
        }
        #endregion

        #region LoadData显示
        private void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = PatientInfo.PATIENT_ID.Trim();   //住院号
            int VisitId = PatientInfo.VISIT_ID;    //住院次数
            int FormId = (int)FormType.FallMTP;    //表单ID
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, this.StatDate.DateTime, this.EndDate.DateTime);
            DataTable dt = new DataTable();
            dt.Columns.Add("hulicuoshi1", typeof(string));
            dt.Columns.Add("hulicuoshi2", typeof(string));
            dt.Columns.Add("hulicuoshi3", typeof(string));
            dt.Columns.Add("hulicuoshi4", typeof(string));
            dt.Columns.Add("hulicuoshi5", typeof(string));
            dt.Columns.Add("hulicuoshi6", typeof(string));
            dt.Columns.Add("hulicuoshi7", typeof(string));
            dt.Columns.Add("hulicuoshi8", typeof(string));
            dt.Columns.Add("hulicuoshi9", typeof(string));
            dt.Columns.Add("hulicuoshi10", typeof(string));
            dt.Columns.Add("hulicuoshi11", typeof(string));
            dt.Columns.Add("hulicuoshi12", typeof(string));
            dt.Columns.Add("hulicuoshi13", typeof(string));
            dt.Columns.Add("hulicuoshi14", typeof(string));
            dt.Columns.Add("hulicuoshi15", typeof(string));
            dt.Columns.Add("hulicuoshi16", typeof(string));
            dt.Columns.Add("hulicuoshi17", typeof(string));
            dt.Columns.Add("hulicuoshi18", typeof(string));
            dt.Columns.Add("hulicuoshi19", typeof(string));
            dt.Columns.Add("hulicuoshi20", typeof(string));
            dt.Columns.Add("diedaopingfen",typeof(string));
            dt.Columns.Add("riqi", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    FallMTP fmtp = new FallMTP();
                    fmtp = XmlUtil.XMLToObject<FallMTP>(NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    string theTrue = "√";
                    #region 护理措施
                    if (!string.IsNullOrEmpty(fmtp.HuLiCuoShi))
                    {
                        row["hulicuoshi1"] = fmtp.HuLiCuoShi.Split('#')[0].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi2"] = fmtp.HuLiCuoShi.Split('#')[1].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi3"] = fmtp.HuLiCuoShi.Split('#')[2].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi4"] = fmtp.HuLiCuoShi.Split('#')[3].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi5"] = fmtp.HuLiCuoShi.Split('#')[4].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi6"] = fmtp.HuLiCuoShi.Split('#')[5].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi7"] = fmtp.HuLiCuoShi.Split('#')[6].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi8"] = fmtp.HuLiCuoShi.Split('#')[7].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi9"] = fmtp.HuLiCuoShi.Split('#')[8].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi10"] = fmtp.HuLiCuoShi.Split('#')[9].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi11"] = fmtp.HuLiCuoShi.Split('#')[10].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi12"] = fmtp.HuLiCuoShi.Split('#')[11].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi13"] = fmtp.HuLiCuoShi.Split('#')[12].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi14"] = fmtp.HuLiCuoShi.Split('#')[13].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi15"] = fmtp.HuLiCuoShi.Split('#')[14].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi16"] = fmtp.HuLiCuoShi.Split('#')[15].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi17"] = fmtp.HuLiCuoShi.Split('#')[16].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi18"] = fmtp.HuLiCuoShi.Split('#')[17].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi19"] = fmtp.HuLiCuoShi.Split('#')[18].Equals("1") ? theTrue : string.Empty;
                        row["hulicuoshi20"] = fmtp.HuLiCuoShi.Split('#')[19].Equals("1") ? theTrue : string.Empty;
                    }
                    #endregion
                    //跌倒评分
                    row["diedaopingfen"] = fmtp.DieDaoPingFen;
                    //日期
                    row["riqi"] = Convert.ToDateTime(fmtp.RiQi).ToString("yyyy-MM-dd");
                    //ID隐藏，触发事件
                    row["Id"] = record.Id;
                    dt.Rows.Add(row);
                }
            }
            gridControl1.DataSource = dt;
        }
        #endregion

        #region 查询
        private void btn_Search_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 新增
        private void btn_Add_Click(object sender, EventArgs e)
        {
            AddFallNursingMeasuresToPrevent addmtp = new AddFallNursingMeasuresToPrevent(LoginUser, PatientInfo, "");
            addmtp.ShowDialog();
            if (addmtp.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void btn_Alter_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddFallNursingMeasuresToPrevent addmtp = new AddFallNursingMeasuresToPrevent(LoginUser, PatientInfo, Id);
                addmtp.ShowDialog();
                if (addmtp.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 打印
        private void btn_Print_Click(object sender, EventArgs e)
        {
            PrintFallNursingMeasuresToPrevent mp = new PrintFallNursingMeasuresToPrevent(PatientInfo, StatDate.DateTime, EndDate.DateTime);
            mp.ShowDialog();
        }
        #endregion

        #region 删除
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确认要删除么？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    string dateStart = StatDate.Text;
                    string dateEnd = EndDate.Text;
                    BindInfo();
                }
            }
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddFallNursingMeasuresToPrevent addmtpp = new AddFallNursingMeasuresToPrevent(LoginUser, PatientInfo, Id);
                addmtpp.ShowDialog();
                if (addmtpp.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion
    }
}